﻿Imports System.Threading
Imports System.IO
Imports Bluegrams

Public Class MainForm
    Private expr_string As String
    Private last_result As Double
    Public history_list As New ArrayList

#Region "Main Methods"

    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim language As String
        statAbout.Text = "v." & Me.ProductVersion & statAbout.Text
        language = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName
        If language = "de" Then
            conLanguageEng.Checked = False
            conLanguageDe.Checked = True
        Else
            conLanguageEng.Checked = True
            conLanguageDe.Checked = False
        End If
        If My.Settings.Memo <> 0 Then
            statInfo.Text = "Memo"
        End If
        Me.KeyPreview = True
    End Sub

    Private Sub lblMain_TextChanged(sender As Object, e As EventArgs) Handles lblMain.TextChanged
        expr_string = lblMain.Text
        If expr_string.Length > 26 Then
            lblMain.Text = expr_string.Substring(expr_string.Length - 26, 26)
        End If
        lblExpression.Text = ""
        statAbout.Text = "v." & Me.ProductVersion & " by Bluegrams 2016"
        lblMain.Focus()
    End Sub

#End Region

#Region "Keys/ Numpad"

    Private Sub MainForm_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyBase.KeyPress
        If BlueCalc.IsValid(e.KeyChar) Then
            lblMain.Text &= e.KeyChar
        End If
    End Sub

    Private Sub keycheck(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Return Then
            Me.butResult.PerformClick()
        ElseIf e.KeyCode = Keys.Delete Or e.KeyCode = Keys.Escape Then
            Me.butAC.PerformClick()
        ElseIf e.KeyCode = Keys.Back Then
            Me.butDEL.PerformClick()
        ElseIf e.KeyCode = Keys.P Then
            Me.butPi.PerformClick()
        ElseIf e.KeyCode = Keys.R Then
            Me.butFrac.PerformClick()
        ElseIf e.KeyCode = Keys.X Then
            Me.butEXP.PerformClick()
        ElseIf e.KeyCode = Keys.Q Then
            Me.butSqrt.PerformClick()
        ElseIf e.KeyCode = Keys.Y Then
            Me.butPower.PerformClick()
        ElseIf e.KeyCode = Keys.F1 Then
            conHelp_Click(sender, e)
        End If
    End Sub

    Private Sub butANS_Click(sender As Object, e As EventArgs) Handles butANS.Click
        lblMain.Text &= last_result
        lblMain.Focus()
    End Sub

    Private Sub butDEL_Click(sender As Object, e As EventArgs) Handles butDEL.Click
        If lblMain.Text.Length > 0 Then
            lblMain.Text = lblMain.Text.Remove(lblMain.Text.Length - 1, 1)
        End If
        lblMain.Focus()
    End Sub

    Private Sub butAC_Click(sender As Object, e As EventArgs) Handles butAC.Click
        lblMain.Text = ""
        lblExpression.Text = ""
        statAbout.Text = "v." & Me.ProductVersion & " by Bluegrams 2016"
        lblMain.Focus()
    End Sub

    Private Sub but7_Click(sender As Object, e As EventArgs) Handles but7.Click
        lblMain.Text &= "7"
    End Sub

    Private Sub but8_Click(sender As Object, e As EventArgs) Handles but8.Click
        lblMain.Text &= "8"
    End Sub

    Private Sub but9_Click(sender As Object, e As EventArgs) Handles but9.Click
        lblMain.Text &= "9"
    End Sub

    Private Sub but4_Click(sender As Object, e As EventArgs) Handles but4.Click
        lblMain.Text &= "4"
    End Sub

    Private Sub but5_Click(sender As Object, e As EventArgs) Handles but5.Click
        lblMain.Text &= "5"
    End Sub

    Private Sub but6_Click(sender As Object, e As EventArgs) Handles but6.Click
        lblMain.Text &= "6"
    End Sub

    Private Sub but1_Click(sender As Object, e As EventArgs) Handles but1.Click
        lblMain.Text &= "1"
    End Sub

    Private Sub but2_Click(sender As Object, e As EventArgs) Handles but2.Click
        lblMain.Text &= "2"
    End Sub

    Private Sub but3_Click(sender As Object, e As EventArgs) Handles but3.Click
        lblMain.Text &= "3"
    End Sub

    Private Sub but0_Click(sender As Object, e As EventArgs) Handles but0.Click
        lblMain.Text &= "0"
    End Sub

    Private Sub butCom_Click(sender As Object, e As EventArgs) Handles butCom.Click
        lblMain.Text &= My.Resources.comma
    End Sub

    Private Sub butAdd_Click(sender As Object, e As EventArgs) Handles butAdd.Click
        lblMain.Text &= "+"
    End Sub

    Private Sub butSubtract_Click(sender As Object, e As EventArgs) Handles butSubtract.Click
        lblMain.Text &= "-"
    End Sub

    Private Sub butMulti_Click(sender As Object, e As EventArgs) Handles butMulti.Click
        lblMain.Text &= "*"
    End Sub

    Private Sub butDivide_Click(sender As Object, e As EventArgs) Handles butDivide.Click
        lblMain.Text &= "/"
    End Sub

    Private Sub butPower_Click(sender As Object, e As EventArgs) Handles butPower.Click
        lblMain.Text &= "^"
    End Sub

    Private Sub butSqrt_Click(sender As Object, e As EventArgs) Handles butSqrt.Click
        lblMain.Text &= "√"
    End Sub

    Private Sub butEXP_Click(sender As Object, e As EventArgs) Handles butEXP.Click
        lblMain.Text &= "*10^"
    End Sub

    Private Sub butPi_Click(sender As Object, e As EventArgs) Handles butPi.Click
        lblMain.Text &= "π"
    End Sub

    Private Sub butE_Click(sender As Object, e As EventArgs) Handles butE.Click
        lblMain.Text &= "e"
    End Sub

#End Region

#Region "Calculate/ History"

    Private Sub butFrac_Click(sender As Object, e As EventArgs) Handles butFrac.Click
        butResult_Click(sender, e)
        If lblMain.Text <> "" And lblMain.Text <> "ERROR" Then
            Dim old_expr As String = lblExpression.Text.Substring(0, lblExpression.Text.Length - 2)
            If last_result = 0 Then
                lblMain.Text = "ERROR"
            Else
                last_result = 1 / last_result
                lblMain.Text = last_result
            End If
            lblExpression.Text = "1/(" & old_expr & ") ="
            history_list.RemoveAt(history_list.Count - 1)
            history_list.Add(lblExpression.Text & lblMain.Text)
        End If
        lblMain.Focus()
    End Sub

    Private Sub butResult_Click(sender As Object, e As EventArgs) Handles butResult.Click
        If expr_string <> "" Then
            Dim expr_calc As String = expr_string
            expr_string = expr_string.Replace("√", "sqrt")
            expr_string = expr_string.Replace("π", "pi")
            Dim result As Double
            If BlueCalc.TryEvaluate(expr_string, result) = False Then
                lblMain.Text = "ERROR"
            Else
                lblMain.Text = result
                last_result = result
                history_list.Add(expr_calc & " = " & result)
            End If
            lblExpression.Text = expr_calc & " ="
        End If
    End Sub

    Private Sub History_Click(sender As Object, e As EventArgs) Handles conHistory.Click, butHistory.Click
        Dim History_Window As New History_Window
        History_Window.ShowDialog()
        lblMain.Focus()
    End Sub

#End Region

#Region "Editmenu"

    Private Sub conCopy_Click(sender As Object, e As EventArgs) Handles conCopy.Click
        Clipboard.SetText(lblMain.Text)
    End Sub

    Private Sub conPaste_Click(sender As Object, e As EventArgs) Handles conPaste.Click
        If BlueCalc.IsValid(Clipboard.GetText) Then
            Dim clip_text As String = Clipboard.GetText
            clip_text = clip_text.Replace("pi", "π")
            clip_text = clip_text.Replace("sqrt", "√")
            lblMain.Text &= clip_text
        Else
            statAbout.Text = My.Resources.valid
        End If

    End Sub

    Private Sub conDelete_Click(sender As Object, e As EventArgs) Handles conDelete.Click
        butAC_Click(sender, e)
    End Sub

    Private Sub contxtEdit_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles contxtEdit.Opening
        If lblMain.Text <> "" Then
            conCopy.Enabled = True
        Else
            conCopy.Enabled = False
        End If
    End Sub

#End Region

#Region "Memo"

    Private Sub butMemoAdd_Click(sender As Object, e As EventArgs) Handles butMemoAdd.Click, butMemoSubtract.Click
        If expr_string <> "" Then
            expr_string = expr_string.Replace("√", "sqrt")
            expr_string = expr_string.Replace("π", "pi")
            Dim memo_add As Double
            If BlueCalc.TryEvaluate(expr_string, memo_add) Then
                If sender Is butMemoSubtract Then memo_add = -memo_add
                My.Settings.Memo += memo_add
                My.Settings.Save()
                statInfo.Text = "Memo"
                statAbout.Text = "M = " & My.Settings.Memo
            End If
        End If
        lblMain.Focus()
    End Sub

    Private Sub butMemoClear_Click(sender As Object, e As EventArgs) Handles butMemoClear.Click
        My.Settings.Memo = 0
        statInfo.Text = ""
        lblMain.Focus()
    End Sub

    Private Sub butMemoRecall_Click(sender As Object, e As EventArgs) Handles butMemoRecall.Click
        If My.Settings.Memo <> 0 Then
            lblMain.Text &= My.Settings.Memo
        End If
        lblMain.Focus()
    End Sub

#End Region

#Region "Set Language"

    Private Sub conLanguageEng_Click(sender As Object, e As EventArgs) Handles conLanguageEng.Click
        Dim new_culture As New Globalization.CultureInfo("en")
        change_Culture(new_culture, sender, e)
    End Sub

    Private Sub conLanguageDe_Click(sender As Object, e As EventArgs) Handles conLanguageDe.Click
        Dim new_culture As New Globalization.CultureInfo("de")
        change_Culture(new_culture, sender, e)
    End Sub

    Private Sub change_Culture(culture As Globalization.CultureInfo, sender As Object, e As EventArgs)
        Dim old_expr As String = lblExpression.Text
        Dim old_main As String = lblMain.Text
        Thread.CurrentThread.CurrentUICulture = culture
        Thread.CurrentThread.CurrentCulture = culture
        Me.Controls.Clear()
        Me.DataBindings.Clear()
        InitializeComponent()
        MainForm_Load(sender, e)
        lblMain.Focus()
        change_comma(old_main, ",", ".")
        change_comma(old_expr, ",", ".")
        lblMain.Text = old_main
        lblExpression.Text = old_expr
    End Sub

    Private Sub change_comma(ByRef term As String, a As Char, b As Char)
        term = term.Replace(a, "####")
        term = term.Replace(b, a)
        term = term.Replace("####", b)
    End Sub

#End Region

#Region "Options"

    Public Sub conToABC_Click(sender As Object, e As EventArgs) Handles conToABC.Click
        Dim proc_path As String = Path.GetDirectoryName(My.Application.Info.DirectoryPath) & "\miniEditor\miniEditor.exe"
        If File.Exists(proc_path) Then
            Dim hff_process As New System.Diagnostics.Process
            Dim copy_arr As String() = history_list.ToArray(GetType(String))
            Dim tempfile As String = Path.GetTempFileName
            File.WriteAllLines(tempfile, copy_arr)
            hff_process.StartInfo.FileName = proc_path
            hff_process.StartInfo.Arguments = "/print " & tempfile
            hff_process.Start()
            Thread.Sleep(2000)
            File.Delete(tempfile)
        Else
            MessageBox.Show(My.Resources.hffError, "miniCalc", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub conHelp_Click(sender As Object, e As EventArgs) Handles conHelp.Click
        HelpWindow.ShowDialog()
    End Sub

    Private Sub conAbout_Click(sender As Object, e As EventArgs) Handles conAbout.Click
        Info_Window.ShowDialog()
    End Sub

#End Region

End Class
